<?php
/**
 * Created by PhpStorm.
 * User: 农夫
 * Date: 2017/9/11
 * Time: 10:19
 */
namespace app\model;

use think\Model;
use My\CategoryTree;
use My\Tool;
use think\Db;

class Menu extends Model
{
    static $tablue_nmae = 'menu';

    static public function menuTree($where = array())
    {
        $data = Db::name(self::$tablue_nmae)->where($where)->order(array('list_order' => 'ASC'))->select();
        $tree = new CategoryTree;
        $tree->tree($data, 'menu_id');
        $_list = $tree->getArray();
        return $_list;
    }

    /**
     * 查询一条数据
     * @param array $where
     * @param array $field
     * @return array|false|\PDOStatement|string|Model
     */
    static public function findService($where = array(), $field = array())
    {
        $return = Db::name(self::$tablue_nmae)->where($where)->field($field)->find();
        return $return;
    }

    /**
     * 删除数据
     * @param array $where
     * @return int
     */
    static public function deleteService($where = array())
    {
        $return = Db::name(self::$tablue_nmae)->where($where)->delete();
        return $return;
    }

    /**
     * 添加数据
     * @param array $data
     * @return $this
     */
    static public function addService($data = array())
    {
        $return = Db::name(self::$tablue_nmae)->insertGetId($data);
        return $return;
    }

    /**
     * 更新数据
     * @param array $where
     * @param array $data
     * @return $this
     */
    static public function updateServer($where = array(), $data = array())
    {
        $return = Db::name(self::$tablue_nmae)->where($where)->update($data);
        return $return;
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param array $field
     * @return false|\PDOStatement|string|\think\Collection
     */
    static public function listService($where = array(), $field = array())
    {
        $return = Db::name(self::$tablue_nmae)->where($where)->field($field)->select();
        return $return;
    }

    /**
     * 权限查询所有数据列表
     * @param array $where
     * @param array $field
     * @return false|\PDOStatement|string|\think\Collection
     */
    static public function listAuthService($where = array(), $field = array())
    {
        $return = Db::name(self::$tablue_nmae)->where($where)->field($field)->select();
        return $return;
    }

    /**
     * 当前位置
     * @param array $where
     * @param array $data
     * @return array
     */
    static public function listCurrentService($where = array(), $data = array())
    {
        $list = Db::name(self::$tablue_nmae)->where($where)->field(array('menu_id', 'pid', 'title'))->select();
        $categoryTree = new CategoryTree();
        $menu_list = $categoryTree->catePath($list, $data['menu_id']);
        return $menu_list;
    }

    /**
     * 数据分页
     * @param array $where
     * @param array $field
     * @return \think\Paginator
     */
    static public function pageService($where = array(), $field = array())
    {
        $return = Db::name(self::$tablue_nmae)->where($where)->field($field)->paginate(pagesize, false, ['query' => request()->param()]);
        return $return;
    }


    //添加权限使用
    static public function menuGroupTree($rules = null)
    {
        $lists = Db::name(self::$tablue_nmae)->where(array('status' => 1))->order(array('list_order' => 'ASC'))->select();
        $arr = array();
        foreach ($lists as $key => $val) {
            $_t = $val;
            $_t['check_rules'] = in_array($val['menu_id'], Tool::stringArray($rules)) ? 1 : 0;
            $arr[] = $_t;
        }
        $categoryTree = new CategoryTree();
        $menu_list = self::authGroupAccessList($categoryTree->categoryRulesList($arr, 0));
        return $menu_list;
    }

    //添加权限使用
    static private function authGroupAccessList($data)
    {
        $html = '';
        foreach ($data as $key => $val) {
            if ($val['check_rules'] == 1) {
                $checked = 'checked';
            } else {
                $checked = '';
            }
            if (empty($val['children_data'])) {
                $html .= '<li><label><input type="checkbox"  ' . $checked . '  class="access-rules pid_' . $val['menu_id'] . '" pid="' . $val['pid'] . '" value="' . $val['menu_id'] . '" name="rules[]"/>' . $val['title'] . '</label></li>';
            } else {
                $html .= '<li><label><input type="checkbox"  ' . $checked . '  class="access-rules pid_' . $val['menu_id'] . '" pid="' . $val['pid'] . '"  value="' . $val['menu_id'] . '" name="rules[]"/>' . $val['title'];
                $html .= self::authGroupAccessList($val['children_data']);
                $html .= '</label></li>';
            }
        }
        return $html ? '<ul>' . $html . '</ul>' : $html;
    }

}